<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\model\CategoryModel;
use think\App;

class Category extends Base {

    private $categoryModel;

    private $req;

    private $page;

    private $version;

    private $limit = 20;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page']  : 1;

        $this->version = isset($this->req['version']) ? $this->req['version']  : 1;

        $this->categoryModel = new CategoryModel();
    }



    public function getList()
    {
        $this->version == "v2" ? $this->getList_v2() : $this->getList_v1();
    }


    private function getList_v1()
    {
        $where= [];
        $where[] =["category_status","=",1];
        $where[] =["category_pid","=",0];

        $list = $this->categoryModel->getList($where,$this->page,$this->limit);

        $total = $this->categoryModel->getTotal($where);

        return $this->succData($total,$list);
    }


    private function getList_v2()
    {
        $where= [];
        $where[] =["category_status","=",1];
        if(isset($this->req["category_pid"]) && !empty($this->req["category_pid"])){
            $where[] =["category_pid","=",$this->req["category_pid"]];
        }

        $list = $this->categoryModel->getList($where,$this->page,$this->limit);

        $total = $this->categoryModel->getTotal($where);

        return $this->succData($total,$list);
    }

}
